<template>
	<view class="page">
		<view v-if="salesCompanyId">
			<view class="address-item" v-for="(item,index) in companydetailInfo.contacts" :key="index" v-if="item.type == 1" @click="selectedChange(item)">
				<view class="address-info">
					<view class="name">
						{{item.name}} {{item.phone}}
					</view>
					<view class="address">
						{{item.city}}{{item.address}}
					</view>
				</view>
				<view class="item-bottom">
					<view class="left-box">
						<image class="radio" src="/static/image/radio_icon.png" mode="aspectFit" v-if="item.flag != 0 || item.type != 1" @click.stop="defaultChange(item.id)"></image>
						<image class="radio" src="/static/image/radio_selected_icon.png" mode="aspectFit" v-if="item.flag == 0 && item.type == 1"></image>
						<view class="radio-title" @click.stop="defaultChange(item.id)">
							默认地址
						</view>
					</view>
					<view class="right-box" @click.stop="deleteAddress(item.id)">
						删除
					</view>
				</view>
			</view>
		</view>
		
		<view v-else>
			<view class="address-item" v-for="(item,index) in companydetailInfo.contacts" :key="index" v-if="item.type == 2" @click="selectedChange(item)">
				<view class="address-info">
					<view class="name">
						{{item.name}} {{item.phone}}
					</view>
					<view class="address">
						{{item.city}}{{item.address}}
					</view>
				</view>
				<view class="item-bottom">
					<view class="left-box">
						<image class="radio" src="/static/image/radio_icon.png" mode="aspectFit" v-if="item.flag != 0 || item.type != 2" @click.stop="defaultChange(item.id)"></image>
						<image class="radio" src="/static/image/radio_selected_icon.png" mode="aspectFit" v-if="item.flag == 0 && item.type == 2"></image>
						<view class="radio-title" @click.stop="defaultChange(item.id)">
							默认地址
						</view>
					</view>
					<view class="right-box" @click.stop="deleteAddress(item.id)">
						删除
					</view>
				</view>
			</view>
		</view>
		
		<view class="clearbottom">
			
		</view>
		<view class="button-box">
		 	<view class="button" @click="jumpAdd">
		 		新增地址
		 	</view>
		</view>
	</view>
</template>

<script>
	import * as api from '@/utils/api.js';
	export default {
		data() {
			return {
				companydetailInfo:{},
				salesCompanyId:''
			}
		},
		onLoad(option) {
			this.salesCompanyId = option.id  // 订单发货页传入的id
			this.initList();
		},
		onShow() {
			let refreshPage = getApp().globalData.refreshPage;
			if(refreshPage == 'address'){
				this.initList();
				getApp().globalData.refreshPage = null;
			}
		},
		methods: {
			/**
			 * 选中卡片触发事件
			 */
			selectedChange:function(item){
				console.log(item);
				getApp().globalData.selected = item;
				uni.navigateBack();
			},
			/**
			 * 默认地址改变触发事件
			 */
			defaultChange:function(id){
				api.contactupdateDefault({'id':id}).then(res=>{
					this.initList();
				})
			},
			/**
			 * 删除地址触发事件
			 */
			deleteAddress:function(id){
				let that = this;
				uni.showModal({
					title: '提示',
					content: '是否确认删除该地址？',
					success: function (res) {
						if (res.confirm) {
							api.contactdelete({ids:id}).then(res=>{
								uni.showToast({
									title: '删除成功',
									mask:true,
									duration: 2000,
									complete() {
										setTimeout(function(){
											that.initList()
										},2000)
									}
								});
							})
						} else if (res.cancel) {
						}
					}
				});
				
			},
			/**
			 * 初始化地址列表
			 */
			initList:function(){
				let userinfo = getApp().globalData.userInfo;
				let data = {};
				if(userinfo.employee){
				userinfo.employee.forEach(res=>{
					if(res.isDefault == "0"){
						data = res;
					}
				})
				}
				
				if(this.salesCompanyId){
					api.companyDetail({id:this.salesCompanyId}).then(res=>{
						this.companydetailInfo = res.data;
					})
				}else{
					api.companyDetail({id:data.tenantId}).then(res=>{
						this.companydetailInfo = res.data;
					})
				}
			},
			jumpAdd:function(){
				uni.navigateTo({
					url: '/subcontractList/order/address/add/add'
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.page{
		background-color: #fafafa;
		width: 100%;
		min-height: 100vh;
		overflow: hidden;
	}
	.address-item{
		margin: 20rpx;
		padding:0 20rpx;
		box-sizing: border-box;
		background-color: white;
		overflow: hidden;
		.name{
			font-size: 32rpx;
			margin-top: 20rpx;
		}
		.address{
			color: #929292;
			font-size: 28rpx;
			line-height:60rpx;
		} 
		.item-bottom{
			border-top: 1px solid #f0f0f0;
			display: flex;
			justify-content: space-between;
			font-size: 28rpx;
			height: 70rpx;
			line-height: 70rpx;
			color: #929292;
			margin-top: 10rpx;
			.left-box{
				display: flex;
				.radio{
					margin:15rpx 0;
					margin-right:10rpx;
					width: 40rpx;
					height: 40rpx;
				}
			}
		}		
	}
	.clearbottom{
		height: calc(constant(safe-area-inset-bottom) + 120rpx);
		height: calc(env(safe-area-inset-bottom) + 120rpx);
	}
	.button-box{
		background-color: #fafafa;
		width: 100%;
		position: fixed;
		bottom: 0;
		left: 0;
		box-sizing: border-box;
		padding:20rpx;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 20rpx);
		padding-bottom: calc(env(safe-area-inset-bottom) + 20rpx);
		.button{
			text-align: center;
			line-height: 80rpx;
			border-radius: 10rpx;
			color: white;
			background-color: #4169e1;
			font-size: 28rpx;
		}
	}
</style>
